'use strict';

const Controller = require('egg').Controller;

class TravelNoteController extends Controller {
  /**
   * 创建游记
   * POST /api/travel-notes
   */
  async create() {
    const { ctx, service } = this;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const noteData = {
      ...ctx.request.body,
      user_id: tokenData.userId
    };

    const result = await service.travelNote.createNote(noteData);
    ctx.body = result;
  }

  /**
   * 更新游记
   * PUT /api/travel-notes/:id
   */
  async update() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.updateNote(
      parseInt(id),
      tokenData.userId,
      ctx.request.body
    );
    ctx.body = result;
  }

  /**
   * 删除游记
   * DELETE /api/travel-notes/:id
   */
  async delete() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.deleteNote(
      parseInt(id),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 获取游记详情
   * GET /api/travel-notes/:id
   */
  async getDetail() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 获取当前用户ID（如果已登录）
    let userId = null;
    if (token) {
      const tokenData = service.auth.verifyToken(token);
      if (tokenData) {
        userId = tokenData.userId;
      }
    }

    const result = await service.travelNote.getNoteDetail(
      parseInt(id),
      userId
    );
    ctx.body = result;
  }

  /**
   * 获取游记列表
   * GET /api/travel-notes
   */
  async getList() {
    const { ctx, service } = this;
    const params = ctx.query;

    const result = await service.travelNote.getNoteList(params);
    ctx.body = result;
  }

  /**
   * 获取我的游记列表
   * GET /api/travel-notes/my
   */
  async getMyNotes() {
    const { ctx, service } = this;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const params = {
      ...ctx.query,
      user_id: tokenData.userId
    };

    const result = await service.travelNote.getNoteList(params);
    ctx.body = result;
  }

  /**
   * 点赞游记
   * POST /api/travel-notes/:id/like
   */
  async like() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.likeNote(
      parseInt(id),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 取消点赞游记
   * DELETE /api/travel-notes/:id/like
   */
  async unlike() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.unlikeNote(
      parseInt(id),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 收藏游记
   * POST /api/travel-notes/:id/favorite
   */
  async favorite() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.favoriteNote(
      parseInt(id),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 取消收藏游记
   * DELETE /api/travel-notes/:id/favorite
   */
  async unfavorite() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.unfavoriteNote(
      parseInt(id),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 获取用户收藏的游记列表
   * GET /api/travel-notes/favorite/list
   */
  async getFavoriteNotes() {
    const { ctx, service } = this;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const params = ctx.query;

    const result = await service.travelNote.getUserFavoriteNotes(
      tokenData.userId,
      params
    );
    ctx.body = result;
  }

  /**
   * 获取用户点赞的游记列表
   * GET /api/travel-notes/like/list
   */
  async getLikeNotes() {
    const { ctx, service } = this;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const params = ctx.query;

    const result = await service.travelNote.getUserLikeNotes(
      tokenData.userId,
      params
    );
    ctx.body = result;
  }

  /**
   * 添加评论
   * POST /api/travel-notes/:id/comments
   */
  async addComment() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const commentData = {
      ...ctx.request.body,
      note_id: parseInt(id),
      user_id: tokenData.userId
    };

    const result = await service.travelNote.addComment(commentData);
    ctx.body = result;
  }

  /**
   * 获取评论列表
   * GET /api/travel-notes/:id/comments
   */
  async getComments() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const params = ctx.query;

    const result = await service.travelNote.getCommentList(
      parseInt(id),
      params
    );
    ctx.body = result;
  }

  /**
   * 删除评论
   * DELETE /api/travel-notes/comments/:commentId
   */
  async deleteComment() {
    const { ctx, service } = this;
    const { commentId } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    const result = await service.travelNote.deleteComment(
      parseInt(commentId),
      tokenData.userId
    );
    ctx.body = result;
  }

  /**
   * 审核游记（管理员）
   * PUT /api/travel-notes/:id/audit
   */
  async audit() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;
    const { status, reason } = ctx.request.body;

    // 验证token和管理员权限
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    if (tokenData.role !== 'admin') {
      ctx.body = {
        success: false,
        message: '无管理员权限'
      };
      return;
    }

    const result = await service.travelNote.auditNote(
      parseInt(id),
      status,
      reason
    );
    ctx.body = result;
  }

  /**
   * 增加游记浏览量（埋点）
   * POST /api/travel-notes/:id/view
   */
  async increaseViewCount() {
    const { ctx, service } = this;
    const { id } = ctx.params;

    const result = await service.travelNote.increaseViewCount(parseInt(id));
    ctx.body = result;
  }

  /**
   * 管理员更新游记（设置精选、置顶等）
   * PUT /api/travel-notes/:id/admin
   */
  async adminUpdate() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    const { token } = ctx.request.headers;

    // 验证token和管理员权限
    const tokenData = service.auth.verifyToken(token);
    if (!tokenData) {
      ctx.body = {
        success: false,
        message: '未登录或token无效'
      };
      return;
    }

    if (tokenData.role !== 'admin') {
      ctx.body = {
        success: false,
        message: '无管理员权限'
      };
      return;
    }

    const result = await service.travelNote.adminUpdateNote(
      parseInt(id),
      ctx.request.body
    );
    ctx.body = result;
  }
}

module.exports = TravelNoteController;

